/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fei.ClassDiagram.WindowComponents;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JPanel;
import org.fei.ClassDiagram.drawing.Package.PackageDrawing;

/**
 *
 * @author Tomas
 */
public class PackageDiagram extends JPanel  implements MouseListener, MouseMotionListener {
    private ArrayList<PackageDrawing> packages;
    private PackageDrawing actual;
    private int tmpX, tmpY;
    
    public PackageDiagram(ArrayList<PackageDrawing> packages) {
        this.packages = packages;
        //Message.showMessage(String.valueOf(packages.isEmpty()));
        this.setBackground(Color.WHITE);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }
    
    @Override
    public void paint(Graphics g) {
        super.paint(g);

        for (PackageDrawing p : packages) {
            p.draw(g);
        }
        
        g.dispose();
    }
    
        @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        for (PackageDrawing d: this.packages) {
            if (d.isOver(e.getX(), e.getY())) {
                actual = d;
                tmpX = (e.getX()-actual.getX());
                tmpY = (e.getY()-actual.getY());
            }
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        actual = null;
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (actual != null) {
            actual.setX(e.getX() - tmpX);
            actual.setY(e.getY() - tmpY);
            this.repaint();
        }
        
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    
    public void setDiagramSize(int width, int height) {
        this.setPreferredSize(new Dimension(width, height));
    }
}
